import { createRouter, createWebHistory } from 'vue-router'
import NotFound from '@/views/error/NotFound'
import PkIndexView from '@/views/pk/PkIndexView'
import RankListView from '@/views/ranklist/RankListView'
import RecordView from '@/views/record/RecordView'
import UserBotView from '@/views/user/bot/UserBotView'
import UserAccountLoginView from '../views/user/account/UserAccountLoginView'
import UserAccountRegisterView from '../views/user/account/UserAccountRegisterView'
import store from "../store/index"

const routes = [
  {
    path: '/',
    name: 'home',
    redirect: "/pk/",
    meta: {
      rerquestAhtu: true,
    }
  },
  {
    path: '/pk/',
    name: 'pk_index',
    component: PkIndexView,
    meta: {
      rerquestAhtu: true,
    }
  },
  {
    path: '/ranklist/',
    name: 'ranklist_index',
    component: RankListView,
    meta: {
      rerquestAhtu: true,
    }
  },
  {
    path: '/record/',
    name: 'record_index',
    component: RecordView,
    meta: {
      rerquestAhtu: true,
    }
  },
  {
    path: '/user/bot/',
    name: 'user_bot_index',
    component: UserBotView,
    meta: {
      rerquestAhtu: true,
    }
  },
  {
    path: "/user/account/login/",
    name: "user_account_login",
    component: UserAccountLoginView,
    meta: {
      rerquestAhtu: false,
    }
  },
  {
    path: "/user/account/register/",
    name: "user_account_register",
    component: UserAccountRegisterView,
    meta: {
      rerquestAhtu: false,
    }
  },
  {
    path: '/404/',
    name: '404',
    component: NotFound,
    meta: {
      rerquestAhtu: false,
    }
  },
  {
    path: '/:catchAll(.*)',
    redirect: "/404/"
  },
]

const router = createRouter({
  history: createWebHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  if ( to.meta.rerquestAhtu && !store.state.user.is_login ) {
    next({name: "user_account_login"})
  } else {
    next();
  }
})

export default router
